<?php 
/*************** AVTAR UPLOAD SCRIPT ******************************/
//By nay27uk
// License: Free
/***********************************************/
include 'includes/common.inc.php';

$UP_ERR = '';
$query = "SELECT * FROM " . $DBPrefix . "users WHERE id = " . $user->user_data['id'];
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
if($_POST['Submit'] == $MSG['77_0031'])
{

	// This is the unique user_id
	$id = $user->user_data['id'];

	if (!empty ($_FILES['ifile']['tmp_name']))
	{

		/* Thumbnail class is required */

		//include_once('phpthumb/ThumbLib.inc.php');
		include_once('includes/class.images.helper.php');

		/* GetImageSize() function pulls out valid info about image such as image type, height etc. If it fails
		 then it is not valid image. */

		if (!getimagesize($_FILES['ifile']['tmp_name']))
		{
			$UP_ERR = "Error - Invalid Image File.";

		} else {

			$imgtype = array('1' => '.gif', '2' => '.jpg' , '3' => '.png');

			// extract the width and height of image
			 
			list($width, $height, $type, $attr) = getimagesize($_FILES['ifile']['tmp_name']);

			// Extract the image extension

			switch ($type)
			{
				case 1: $ext='.gif'; break;
				case 2: $ext='.jpg'; break;
				case 3: $ext='.png'; break;
			}
			// Dont allow gif files to upload as it may  contain harmful code

			if ( $ext == '.gif') {
				//die("Sorry - GIF not allowed. Please use only PNG or JPEG formats");
			}

			/* Specify maximum height and width of users uploading image */

			if ($width > 3000 || $height > 3000)
			{
				$UP_ERR = "ERROR: Maximum width and height exceeded. (max 3000x3000 pixels)";
				 
			} else {
				/* Specify maximum file size here in bytes */
				 
				if ($_FILES['ifile']['size'] > 500000 )
				{
					$UP_ERR = "Error: Large File size. (max 500kb)";

				} else {
					 
					/******** IMAGE RESIZING *********************/
					// Before we start resizing, we first have to move the image file to server
					// save it there under a unique name and then do the final resizing and save the resized image.

					// Specify which directory you want to upload. It should be a subfolder where the script is present
					// We also generate a unique name for picture FILE-USERID-XXX where xxx is random number
					// The uploads folder must have writable permissions.

					$uploaddir = 'uploaded/avatar/';
					$secondname = rand(100,99);
					$uploadfile =  $uploaddir . "img-$id-$secondname". $ext;

					if (!move_uploaded_file($_FILES['ifile']['tmp_name'], $uploadfile ))
					{
						$UP_ERR = "Error moving the uploaded file";
					} else {

						if (!empty($user->user_data['avatar']))
							@unlink($user->user_data['avatar']);

						$width      = ImagesHelper::width($uploadfile) / 120;
						$height     = ImagesHelper::height($uploadfile) / 120;

						if ($width > $height)
							$ratio = ceil(100/$width);
						else
							$ratio = ceil(100/$height);

						$prefix = 'sc_'; // Приставка к имени файла от масштабированного
						$format = 'jpg'; // формат для сохранения изображения
						if($scaled_image  = ImagesHelper::scale($uploadfile,$ratio)) {
							// сохранение полученного изображения
							ImagesHelper::save($scaled_image, $uploadfile,$prefix,$format);
							@unlink($uploadfile);
							$uploadfile =  $uploaddir . $prefix . "img-$id-$secondname" . "." . $format;


							//MySQL query to update avatar filename in the database. You need to create a field avatar
							$query = "UPDATE " . $DBPrefix . "users SET avatar = '" . $uploadfile . "' WHERE id = " . $user->user_data['id'];
							$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
							$user->user_data['avatar'] = $uploadfile;
						} else {
							$UP_ERR = "Error resize the uploaded file";
						}




						//$thumb->destruct();
					}
				}
			}
		}
	}
}
if (!empty($UP_ERR)) {
	$_SESSION['AVATAR_UPL_ERR'] = $UP_ERR;
} else {
	$_SESSION['AVATAR_UPL_ERR'] = '';
}
header ("Location: user_menu.php?cptab=avatarupload");
exit;

?>

